package com.tensquare.test.base.Controller;

import com.tensquare.test.base.pojo.Label;
import com.tensquare.test.base.service.LableService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/label")
public class LabelController {
    @Autowired
    private LableService lableService;

    @RequestMapping(value ="search/{page}/{size}", method = RequestMethod.POST)
    public Result pageQuery(@PathVariable int page,@PathVariable int size ,@RequestBody Label label) {
        Page<Label> pageData = lableService. pageQuery(page,size,label) ;

        return new Result(true, StatusCode.OK, "标签分页成功",new PageResult<Label>(pageData.getTotalElements(),pageData.getContent()));
    }

    @GetMapping
    public Result findAll() {
        return new Result(true, StatusCode.OK, "查询成功", lableService.findAll());
    }

    @GetMapping("/{labelId}")
    public Result findById(@PathVariable("labelId") String lableId) {
       // int i =1/0;
        return new Result(true, StatusCode.OK, "查询成功", lableService.findById(lableId));
    }

    @PostMapping
    public Result save(@RequestBody Label label) {
        lableService.save(label);
        return new Result(true, StatusCode.OK, "新增成功");
    }

    @RequestMapping(value ="/{labelId}", method = RequestMethod.PUT)
    public Result update(@PathVariable String labelId, @RequestBody Label label) {
           label.setId(labelId);
           lableService.update(label);
        return new Result(true, StatusCode.OK, "更新成功");
    }

    @RequestMapping(value = "/{labelId}", method = RequestMethod.DELETE)
    public Result deleteById(@PathVariable String labelId) {
        lableService.deleteById(labelId);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    @RequestMapping(value ="/search", method = RequestMethod.POST)
    public Result search(@RequestBody Label label) {

        return new Result(true, StatusCode.OK, "搜索成功",lableService.serach(label));
    }

}
